|
1
|
|
|
|
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
var request = require('request-promise'); |
|
5
|
|
|
|
|
6
|
|
|
var API_VERSION = '0.2'; |
|
7
|
|
|
var ENDPOINT_BASE = `https://api.sandcage.com/${API_VERSION}/`; |
|
8
|
|
|
|
|
9
|
|
|
var END_POINTS = { |
|
10
|
|
|
GET_INFO: 'get-info', |
|
11
|
|
|
LIST_FILES: 'list-files', |
|
12
|
|
|
SCHEDULE_TASKS: 'schedule-tasks', |
|
13
|
|
|
DESTROY_FILES: 'destroy-files', |
|
14
|
|
|
}; |
|
15
|
|
|
|
|
16
|
|
|
class Sandcage { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param {Object} options options for the service |
|
20
|
|
|
* @param {String} options.apikey Api key, mandatory |
|
21
|
|
|
*/ |
|
22
|
|
|
constructor(options) { |
|
23
|
|
|
options = options || {}; |
|
24
|
|
|
if (!options.apiKey) { |
|
25
|
|
|
throw new Error('Provide your SandCage API Key.', 'MissingKey'); |
|
26
|
|
|
} |
|
27
|
|
|
this.apiKey = options.apiKey; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The "get-info" service |
|
32
|
|
|
* @param {Object} params the parameters to pass to the request |
|
33
|
|
|
* @param {String} [params.request_id] The request_id that was returned in the request for which you want to get the |
|
34
|
|
|
* status. |
|
35
|
|
|
If provided, the response will include details for all the files that where part of the request. |
|
36
|
|
|
If a value is not provided for this parameter, then an array of files must be provided. |
|
37
|
|
|
* @param {Array} [params.files] An array of files. |
|
38
|
|
|
If a value is not provided for this parameter, then a value must be provided for the "request_id" parameter. |
|
39
|
|
|
If a value is provided for the "request_id" parameter, then this array of files will be disregarded. |
|
40
|
|
|
* @param {String} [params.files.file_token] The file_token of the file for which you want to get the status. |
|
41
|
|
|
* This returned as part of the response of a request to the schedule-tasks service. |
|
42
|
|
|
* @param {Function} [callback] callback an optional callback to execute when the API call is complete |
|
43
|
|
|
* @returns {Promise} |
|
44
|
|
|
*/ |
|
45
|
|
|
getInfo(params, callback) { |
|
46
|
|
|
if (params === null || Object.keys(params).length === 0) { |
|
47
|
|
|
var err = new Error('No params provided'); |
|
48
|
|
|
if (callback) { |
|
49
|
|
|
callback(err); |
|
50
|
|
|
} |
|
51
|
|
|
return Promise.reject(err); |
|
52
|
|
|
} |
|
53
|
|
|
return this._sendRequest(END_POINTS.GET_INFO, params, '', callback); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The "list-files" service |
|
59
|
|
|
* @param {Object} params the parameters to pass to the request |
|
60
|
|
|
* @param {String} [params.directory] The relative directory to search through. Default is the root directory. |
|
61
|
|
|
* @param {Number} [params.page] The targeted paginated results. Default is 1. |
|
62
|
|
|
* @param {Number} [params.results_per_page] The amount of file entries that should be returned per request. |
|
63
|
|
|
* Default is 100. |
|
64
|
|
|
* @param {Function} [callback] callback an optional callback to execute when the API call is complete |
|
65
|
|
|
* @returns {Promise} |
|
66
|
|
|
*/ |
|
67
|
|
|
listFiles(params, callback) { |
|
68
|
|
|
if (params === null || Object.keys(params).length === 0) { |
|
69
|
|
|
var err = new Error('No params provided'); |
|
70
|
|
|
if (callback) { |
|
71
|
|
|
callback(err); |
|
72
|
|
|
} |
|
73
|
|
|
return Promise.reject(err); |
|
74
|
|
|
} |
|
75
|
|
|
return this._sendRequest(END_POINTS.LIST_FILES, params, '', callback); |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* The "schedule-tasks" service |
|
80
|
|
|
* @param {Object} params the parameters to pass to the request |
|
81
|
|
|
* @param {Array} params.jobs An array of tasks. |
|
82
|
|
|
* This will only be included as part of the response if the value of "status" was not "error". |
|
83
|
|
|
* @param {String} [callbackEndpoint] an optional callback endpoint, to which a request will be sent whenever there is an update for any of the tasks included in this request. See https://www.sandcage.com/docs/0.2/schedule_tasks#callbacks for an example |
|
84
|
|
|
* @param {Function} [callback] callback an optional callback to execute when the API call is complete |
|
85
|
|
|
* @returns {Promise} |
|
86
|
|
|
*/ |
|
87
|
|
|
scheduleFiles(params, callbackEndpoint, callback) { |
|
88
|
|
|
|
|
89
|
|
|
if (params === null || Object.keys(params).length === 0) { |
|
90
|
|
|
var err = new Error('No params provided'); |
|
91
|
|
|
if (callback) { |
|
92
|
|
|
callback(err); |
|
93
|
|
|
} |
|
94
|
|
|
return Promise.reject(err); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (callbackEndpoint == null) { |
|
98
|
|
|
callbackEndpoint = ''; |
|
99
|
|
|
} |
|
100
|
|
|
return this._sendRequest(END_POINTS.SCHEDULE_TASKS, params, callbackEndpoint, callback); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* The "destroy-files" service |
|
106
|
|
|
* @param {Object} params the parameters to pass to the request |
|
107
|
|
|
* @param {Array} params.files An array of files to be deleted. |
|
108
|
|
|
* @param {String} callbackEndpoint an optional callback endpoint, to which a request will be sent whenever there is an update for any of the tasks included in this request. See https://www.sandcage.com/docs/0.2/destroy_files#callbacks for an example |
|
109
|
|
|
* @param {Function} [callback] callback an optional callback to execute when the API call is complete |
|
110
|
|
|
* @returns {Promise} |
|
111
|
|
|
*/ |
|
112
|
|
|
destroyFiles(params, callbackEndpoint, callback) { |
|
113
|
|
|
if (params === null || Object.keys(params).length === 0) { |
|
114
|
|
|
var err = new Error('No params provided'); |
|
115
|
|
|
if (callback) { |
|
116
|
|
|
callback(err); |
|
117
|
|
|
} |
|
118
|
|
|
return Promise.reject(err); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (callbackEndpoint == null) { |
|
122
|
|
|
callbackEndpoint = ''; |
|
123
|
|
|
} |
|
124
|
|
|
return this._sendRequest(END_POINTS.DESTROY_FILES, params, callbackEndpoint, callback); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
_sendRequest(serviceEndpoint, params, callbackEndpoint, callback) { |
|
128
|
|
|
var payload = Object.assign({ |
|
129
|
|
|
key: this.apiKey |
|
130
|
|
|
}, params); |
|
131
|
|
|
|
|
132
|
|
|
if (callbackEndpoint && callbackEndpoint !== '') { |
|
133
|
|
|
payload['callback_url'] = callbackEndpoint; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
var options = { |
|
137
|
|
|
method: 'POST', |
|
138
|
|
|
uri: ENDPOINT_BASE + serviceEndpoint, |
|
139
|
|
|
headers: { |
|
140
|
|
|
'Content-Type': 'application/x-www-form-urlencoded' |
|
141
|
|
|
}, |
|
142
|
|
|
body: JSON.stringify(payload), |
|
143
|
|
|
json: true |
|
144
|
|
|
}; |
|
145
|
|
|
|
|
146
|
|
|
return request(options) |
|
147
|
|
|
.then(function(result) { |
|
148
|
|
|
if (callback) { |
|
149
|
|
|
return callback(null, result); |
|
150
|
|
|
} else { |
|
151
|
|
|
return result; |
|
152
|
|
|
} |
|
153
|
|
|
}) |
|
154
|
|
|
.catch(function(err) { |
|
155
|
|
|
if (callback) { |
|
156
|
|
|
return callback(err); |
|
157
|
|
|
} else { |
|
158
|
|
|
throw err; |
|
159
|
|
|
} |
|
160
|
|
|
}); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
module.exports = Sandcage; |